<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form-model ref='form' :model='model' :rules='validatorRules' slot='detail'>
        <a-row>
          <a-col :span="24" v-if="modelDefault.examinationPaperId != null">
            <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="">
              <a-button @click="speedAddQuestionsItem" type="primary" icon="plus">快速新增题目</a-button>
              <span style="color: #dd1820;margin-left: 10px;"> * 适用于题目结构一致的选择题、填空题及判断题等</span>
            </a-form-model-item>
          </a-col>
          <!-- 快速新增题型modal -->
          <div>
            <a-modal title="快速新增题型" :visible="speedAddQuestionsItemVisible" @ok="speedAddQuestionsItemOk()" @cancel="speedAddQuestionsItemVisibleCancel" width='50%'>
              <a-row>
                <a-col :span='12'>
                  <a-form-model-item label='所属试卷' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='examinationPaperId'>
                    <j-dict-select-tag type='list' v-model='speedAddQuestionsItemModel.examinationPaperId = modelDefault.examinationPaperId'  style="width: 100%"  disabled="disabled"
                                       dictCode='ach_examination_paper,examination_paper_name,id' placeholder='请输入所属试卷' />
                  </a-form-model-item>
                </a-col>
                <a-col :span="12">
                  <a-form-model-item label='所属题型' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='examPaperProjectItemId'>
                    <!--              <a-input v-model="model.examPaperProjectItemId" placeholder="请输入所属题型分项" ></a-input>-->
                    <j-dict-select-tag type='list' v-model='speedAddQuestionsItemModel.examPaperProjectItemId' style="width: 100%"
                                       :dictCode='queryParamSelect.examPaperProjectItemIdSelect'
                                       placeholder='请输入所属题型分项' />
                  </a-form-model-item>
                </a-col>
                <a-col :span='12'>
                  <a-form-model-item label='试题类型[必填]' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='exaPapQueItemTtype'>
                    <!--              <a-input v-model="model.exaPapQueItemTtype" placeholder="请输入试题类型（1单选 2多选  3简答 4填空 5判断 6阅读 7完型填空 8实验 9手工制作） "  ></a-input>-->
                    <j-dict-select-tag type='list' v-model='speedAddQuestionsItemModel.exaPapQueItemTtype' style="width: 100%"
                                       dictCode='exa_pap_que_item_ttype'
                                       placeholder='请输入试题类型（1单选 2多选  3简答 4填空 5判断 6阅读 7完型填空 8实验 9手工制作）' />
                  </a-form-model-item>
                </a-col>
                <a-col :span='12'>
                  <a-form-model-item label='建议用时(分钟)' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='proposalDuration'>
                    <a-input-number v-model='speedAddQuestionsItemModel.proposalDuration' placeholder='请输入考试建议用时(分钟)' style='width: 100%' />
                  </a-form-model-item>
                </a-col>
                <a-col :span='12'>
                  <a-form-model-item label='开始题号' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='startExaPapQueItemNo'>
                    <a-input v-model='speedAddQuestionsItemModel.startExaPapQueItemNo' placeholder='请输入开始题号'></a-input>
                  </a-form-model-item>
                </a-col>
                <a-col :span='12'>
                  <a-form-model-item label='显示顺序' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='startExaPapQueItemOrder'>
                    <a-input v-model='speedAddQuestionsItemModel.startExaPapQueItemOrder' placeholder='可以与题号一致'></a-input>
                  </a-form-model-item>
                </a-col>
                <a-col :span="12">
                  <a-form-model-item label="单题分数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proItemScore">
                    <a-input-number v-model="speedAddQuestionsItemModel.exaPapQueItemFullMarks" placeholder="请输入单题分数" style="width: 100%" />
                  </a-form-model-item>
                </a-col>
                <a-col :span="12">
                  <a-form-model-item label="生成题数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proItemNum">
                    <a-input-number v-model="speedAddQuestionsItemModel.proItemNum" placeholder="请输入要生成的题数" style="width: 100%" />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-modal>
          </div>
        </a-row>
        <a-row>
          <a-col :span='12'>
            <a-form-model-item label='所属试卷' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='examinationPaperId'>
              <j-dict-select-tag type='list' v-model='model.examinationPaperId' disabled="disabled"
                                 dictCode='ach_examination_paper,examination_paper_name,id' placeholder='请输入所属试卷' />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row type="flex" style="margin-bottom:10px" v-for="(item, index) in orderMainModel.mainCustomerList" :key="index" >
          <div style="width: 100%;background-color: rgb(239 239 239);border-radius: 2px;padding-top: 10px;">
            <div><span style="font-weight: bold;font-size: 15px;margin-left: 10px;color: #00A0E9;"> 题 目: {{ index + 1 }}</span></div>
            <a-col :span='12'>
              <a-form-model-item label='所属题型' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='examPaperProjectItemId'>
                <!--              <a-input v-model="model.examPaperProjectItemId" placeholder="请输入所属题型分项" ></a-input>-->
                <j-dict-select-tag type='list' v-model='item.examPaperProjectItemId'
                                   :dictCode='queryParamSelect.examPaperProjectItemIdSelect'
                                   placeholder='请输入所属题型分项' />
              </a-form-model-item>
            </a-col>
            <a-col :span='12'>
              <a-form-model-item label='试题满分[必填]' :labelCol='labelCol' :wrapperCol='wrapperCol'
                                 prop='exaPapQueItemFullMarks'>
                <a-input-number v-model='item.exaPapQueItemFullMarks' placeholder='请输入试题满分' style='width: 100%' />
              </a-form-model-item>
            </a-col>
            <a-col :span='12'>
              <a-form-model-item label='题号[必填]' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='exaPapQueItemNo'>
                <a-input v-model='item.exaPapQueItemNo' placeholder='请输入题号'></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span='12'>
              <a-form-model-item label='显示顺序[必填]' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='exaPapQueItemOrder'>
                <a-input v-model='item.exaPapQueItemOrder' placeholder='可以与题号一致'></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span='12'>
              <a-form-model-item label='题库试题' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='questionBankItemId'>
                <a-input v-model='item.questionBankItemId' placeholder='请输入题库试题id'></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span='12'>
              <a-form-model-item label='试题类型[必填]' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='exaPapQueItemTtype'>
                <!--              <a-input v-model="model.exaPapQueItemTtype" placeholder="请输入试题类型（1单选 2多选  3简答 4填空 5判断 6阅读 7完型填空 8实验 9手工制作） "  ></a-input>-->
                <j-dict-select-tag type='list' v-model='item.exaPapQueItemTtype'
                                   dictCode='exa_pap_que_item_ttype'
                                   placeholder='请输入试题类型（1单选 2多选  3简答 4填空 5判断 6阅读 7完型填空 8实验 9手工制作）' />
              </a-form-model-item>
            </a-col>
            <a-col :span='12'>
              <a-form-model-item label='建议用时(分钟)' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='proposalDuration'>
                <a-input-number v-model='item.proposalDuration' placeholder='请输入建议用时(分钟)' style='width: 100%' />
              </a-form-model-item>
            </a-col>
<!--            <a-col :span='12'>-->
<!--              <a-form-model-item label='是否启用' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='izOpen'>-->
<!--                <j-dict-select-tag type='list' v-model='item.izOpen = defaultText.izOpen'-->
<!--                                   dictCode='iz_open' placeholder='请选择是否启用' />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
<!--            <a-col :span='12'>-->
<!--              <a-form-model-item label='是否启用' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='delFlag'>-->
<!--                <j-dict-select-tag type='list' v-model='item.delFlag = defaultText.izOpen'-->
<!--                                   dictCode='iz_open' placeholder='请选择是否启用' />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->

            <!--          <a-col :span="12">-->
            <!--            <a-form-model-item label="创建用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">-->
            <!--              <a-input v-model="model.createBy" placeholder="请输入创建人"  ></a-input>-->
            <!--            </a-form-model-item>-->
            <!--          </a-col>-->
            <!--          <a-col :span="12">-->
            <!--            <a-form-model-item label="修改用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateBy">-->
            <!--              <a-input v-model="model.updateBy" placeholder="请输入修改人"  ></a-input>-->
            <!--            </a-form-model-item>-->
            <!--          </a-col>-->

            <a-col :span="24">
              <a-form-model-item style="text-align: center;width: 100%">
                <a-button type="dashed" style="margin-top: 10px" @click="delRowCustom(index)">
                  <a-icon type="minus-circle" /> 删除题目
                </a-button>
              </a-form-model-item>
            </a-col>
          </div>
        </a-row>
        <a-button type="dashed" style="width: 98%;margin-top: 10px" @click="addRowCustom()">
          <a-icon type="plus" /> 添加题目
        </a-button>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { postAction } from '../../../../api/manage'

export default {
  name: 'AchExamPaperQuestionsItemForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/achexampaperquestionsitem/achExamPaperQuestionsItem/add',
        edit: '/achexampaperquestionsitem/achExamPaperQuestionsItem/edit',
        queryById: '/achexampaperquestionsitem/achExamPaperQuestionsItem/queryById',

        //快速新增题目
        speedAddQuestionsItem: '/achexampaperquestionsitem/achExamPaperQuestionsItem/speedAdd',
      },
      queryParamSelect: {
        examPaperProjectItemIdSelect: ''
      },

      //动态增加表单
      orderMainModel: {
        mainCustomerList: [{}],
        jeecgOrderTicketList: [{}]
      },
      //设置传过来的默认值
      defaultData: [],
      //新增时默认值设置
      defaultText: {
        izOpen: 1,
        delFlag: 0,
      },

      //快速新增题型modal
      speedAddQuestionsItemVisible:false,
      speedAddQuestionsItemModel:{},//快速新增题型modal

    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    //动态增加表单
    this.form = this.$form.createForm(this)
  },
  methods: {
    //objectModel 从AchExamPaperProjectItemList的传过来的选择的数据（需要的是试卷id和科目id）
    add(objectModel) {
      //新增初始化设置默认值
      this.defaultText.examinationPaperId = objectModel.id,//所属试卷
      this.modelDefault.examinationPaperId = this.defaultText.examinationPaperId
      // this.modelDefault.delFlag = 0,//默认未删除
      // this.modelDefault.izOpen = 1,//默认启用

      this.defaultData = objectModel
      //objectModel 中的数据为当前已选择的试卷数据
      // this.modelDefault.examinationPaperId = objectModel.id //试题新增时，所属试卷默认赋值为选择试卷的id
      this.edit(this.modelDefault)
      this.paramChange(objectModel.id)
    },
    paramChange(pid) {
      if (pid != null || pid != ""){
        this.queryParamSelect.examPaperProjectItemIdSelect = 'ach_exam_paper_project_item aeppi,aeppi.exa_pap_pro_item_name ,aeppi.id,aeppi.examination_paper_id=' + pid + 'order by aeppi.exa_pap_pro_item_order'
      }
    },
    edit(record) {
      this.defaultText.examinationPaperId = record.id,//所属试卷
      this.model = Object.assign({}, record)
      this.orderMainModel.mainCustomerList.length = 0;//编辑时设默认表单为0
      this.orderMainModel.mainCustomerList.push(this.model);//push后表单为当前编辑的一个
      this.visible = true
      this.paramChange(record.examinationPaperId)
    },
    submitForm() {
      //动态增加表单数据提交，循环依次新增
      if (this.orderMainModel.mainCustomerList.length > 0){
        this.orderMainModel.mainCustomerList.forEach((item, index) => {
          this.model = item
          //提交表单数据
          const that = this;
          // 触发表单验证
          this.$refs.form.validate(valid => {
            if (valid) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                //设置默认值
                this.model.examinationPaperId = this.modelDefault.examinationPaperId;//默认启用
                this.model.izOpen = 1;//默认启用
                this.model.delFlag = 0;//默认未删除
              }else{
                httpurl+=this.url.edit;
                method = 'put';
              }
              httpAction(httpurl,this.model,method).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.$emit('ok');
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.confirmLoading = false;
              })
            }
          })
        });
      }
      //提交完后清空缓存动态表单数据，一面影响下次数据
      this.orderMainModel.mainCustomerList = [{}]
    },

    //动态增加表单
    // 初始化
    close () {
      this.$emit('close');
      this.visible = false;
    },
    handleOk () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.orderMainModel.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          httpAction(httpurl,this.orderMainModel,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })

        }
      })
    },
    handleCancel () {
      this.close()
    },
    addRowCustom () {
      this.orderMainModel.mainCustomerList.push({});
      this.$forceUpdate();
    },
    delRowCustom (index) {
      console.log(index)
      let all = this.orderMainModel
      all['mainCustomerList'].splice(index,1);
      this.orderMainModel.mainCustomerList.splice(index,1);
      this.$forceUpdate();
    },
    addRowTicket () {
      this.orderMainModel.jeecgOrderTicketList.push({});
      // console.log(this.orderMainModel.jeecgOrderTicketList)
      this.$forceUpdate();
    },
    delRowTicket (index) {
      // console.log(index)
      let all = this.orderMainModel
      all['jeecgOrderTicketList'].splice(index,1);
      this.orderMainModel.jeecgOrderTicketList.splice(index,1);
      this.$forceUpdate();
    },

    //快速生成题型
    speedAddQuestionsItem(){
      this.speedAddQuestionsItemVisible = true;
    },
    speedAddQuestionsItemVisibleCancel(){
      this.speedAddQuestionsItemVisible = false;
    },
    //快速生成题型 提交
    speedAddQuestionsItemOk(){
      let speedAddParams = {
        examinationPaperId : this.speedAddQuestionsItemModel.examinationPaperId,//所属试卷id
        examPaperProjectItemId : this.speedAddQuestionsItemModel.examPaperProjectItemId,//上级题型id
        exaPapQueItemTtype : this.speedAddQuestionsItemModel.exaPapQueItemTtype,//题型类型
        startExaPapQueItemNo : this.speedAddQuestionsItemModel.startExaPapQueItemNo,//开始试题编号
        startExaPapQueItemOrder : this.speedAddQuestionsItemModel.startExaPapQueItemOrder,//开始试题顺序
        exaPapQueItemFullMarks : this.speedAddQuestionsItemModel.exaPapQueItemFullMarks,//单题满分
        proposalDuration : this.speedAddQuestionsItemModel.proposalDuration,//单题时长
        proItemNum : this.speedAddQuestionsItemModel.proItemNum,//要添加的题目数量
      }
      postAction(this.url.speedAddQuestionsItem,speedAddParams).then((res) => {
        if (res.success){
          this.$message.success("快速新增已完成")
        } else {
          this.$message.error("新增失败")
        }
        this.speedAddQuestionsItemVisible = false
      });
    },

  }
}
</script>

<style scoped>

</style>